//
//  AY_RankListPage.m
//  AiYue
//
//  Created by boWen on 2023/10/19.
//

#import "AY_RankListPage.h"
#import "AY_RankCell.h"
#import "AY_BookDetailViewController.h"
//排行榜
@interface AY_RankListPage ()<UITableViewDataSource,UITableViewDelegate>

/**
 UITableView 基类
 */
@property (nonatomic,strong) UITableView *tableView;

/**
 UITableView 数据源
 */
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation AY_RankListPage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = COLOR(@"2a2832");
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)setModel:(RankListModel *)model {
    [AYApiRequest getRankBookListWithRankId:model.cId Success:^(AY_RankModel * _Nonnull pkg) {
        self.dataSource = [NSMutableArray arrayWithArray:pkg.data];
        [self.tableView reloadData];
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count == 0 ? 0 : self.dataSource.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.dataSource.count) {
        static NSString *ID = @"UITableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.contentView.backgroundColor = [UIColor whiteColor];
        return cell;
    }else {
        static NSString *ID = @"AY_RankCell";
        AY_RankCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[AY_RankCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!kArrayIsEmpty(self.dataSource)) {
            RankListModel *model = self.dataSource[indexPath.row];
            [cell sendBookModel:model andIndex:indexPath.row];
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataSource.count) {
        return YNUM(50);
    }else {
        return YNUM(116);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    RankListModel *model = self.dataSource[indexPath.row];
    bookDetailVC.bookId = model.cId;
    [self.navigationController pushViewController:bookDetailVC animated:YES];
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        NSMutableArray *dataSource = [NSMutableArray array];
        _dataSource = dataSource;
    }
    return _dataSource;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, YNUM(2), SCREEN_W, SCREEN_H - GK_TABBAR_HEIGHT - YNUM(167) - 44.0) style:UITableViewStylePlain];
        tableView.layer.masksToBounds = YES;
        tableView.layer.cornerRadius = YNUM(12);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.translatesAutoresizingMaskIntoConstraints = NO;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate = self;
        tableView.tableFooterView = [[UIView alloc] init];
        _tableView = tableView;
    }
    return _tableView;
}

@end
